//index.js
//获取应用实例
const app = getApp()
Page({
  //滚动条最最下面
  onReachBottom:function(e){
    console.log("上拉加载数据")
  },
  bottom:function(e){
    console.log("滚动条到最下面了")
  },
  data: {
    img_title_xuanyou:"icons/xuanyou_title.png",
    img_title_renwu:"icons/renwu_title.png",
    // banner图片
    img_banner:"icons/banner.png",
    // 轮播图图片
    imgUrls: [],
    // 大广告图片和文字
    tuijian: [],
    renwu:[],
    xuanyou:[],
    commoncategory:[],
    //当前显示的商品页签ID
    currentItemId:"1",
    //当前页签的提示
    common_tip:"",
    goods:[]
  },

  onLoad: function (options) {
    var that = this;
    // 加载轮播图图片
    console.log("图片地址：" + app.globalData.imgUrl);
    wx.request({
      url: app.globalData.url +'/home/lunbo',
      success:function(data){
        var imgurls  =data.data;
        
        that.setData({
          imgUrls: imgurls
        });
      }
    })
    //获取推荐商品信息
    wx.request({
      url: app.globalData.url +'/home/tuijian',
      success: function (data) {
        var imgurls = data.data;
        for (var i in imgurls) {
          imgurls[i].img = app.globalData.imgUrl + imgurls[i].img
        }
        that.setData({
          tuijian: imgurls
        });
      }
    })
    //查询最近任务
    wx.request({
      url: app.globalData.url +'/home/renwu',
      success: function (data) {
        var imgurls = data.data;
        for (var i in imgurls) {
          imgurls[i].img = app.globalData.imgUrl + imgurls[i].img
        }
        that.setData({
          renwu: imgurls
        });
      }
    })
    // 查询选优信息
    wx.request({
      url: app.globalData.url +'/home/xuanyou',
      success: function (data) {
        var imgurls = data.data;
        for (var i in imgurls) {
          imgurls[i].img = app.globalData.imgUrl + imgurls[i].img
        }
        that.setData({
          xuanyou: imgurls
        });
      }
    })
    //查询下面的商品推荐分类
    wx.request({
      url: app.globalData.url +'/home/commoncategory',
      success: function (data) {
        console.log(data);
        that.setData({
          commoncategory:data.data,
          currentItemId: data.data[0].id,
          common_tip:data.data[0].common_tip
        });
        var cateid = data.data[0].id;
        //获取第一个分类下的商品
        wx.request({
          url: app.globalData.url +'/home/commongoods',
          data:{
            categoryId: cateid
          },
          success: function (data) {
            wx.setStorageSync("cate_"+cateid,data.data);
            var imgurls = data.data;
            for (var i in imgurls) {
              imgurls[i].img = app.globalData.imgUrl + imgurls[i].img
            }
            that.setData({
              goods: imgurls
            });
          }
        })
      }
    })
  },
  // 点击标签切换
  changetag : function(e){
      var that = this;
      //获取选中的标签ID和对应的商品
      var cateId = e.currentTarget.dataset.itemid;
      var goods2 = wx.getStorageSync('cate_' + cateId);
      //如果没有，去后台查询商品
      if (!goods2){
        console.log("获取数据")
        wx.request({
          url: app.globalData.url +'/home/commongoods',
          data: {
            categoryId: cateId
          },
          success: function (data) {
            goods2 = data.data;
            wx.setStorageSync("cate_" + cateId, goods2);
            for (var i in goods2) {
              goods2[i].img = app.globalData.imgUrl + goods2[i].img
            }
            that.setData({
              goods: goods2
            })
          }
        })
      }else{
        this.setData({
          goods: goods2
        })
      }
      
      this.setData({
        currentItemId: cateId,
        common_tip: e.currentTarget.dataset.commontip,
      })
  }
})
